package com.justin.demo;

import com.justin.demo.utils.LogUtils;

/**
 * synchronized 同步锁
 */
public class SellTickets003 implements Runnable {

    //初始化票数
    private static int tickets = 30 ;

    @Override
    public void run() {
        while(true) {
            //synchronized (this) {
            synchronized (SellTickets003.class) {
                if (tickets > 0) {
                    try {
                        //卖票延迟时间
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    LogUtils.info("线程[{}] 正在卖第[{}]张票", Thread.currentThread().getName(), tickets);
                    tickets--;
                }
            }
            // }
        }
    }
}
